<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ProductCategoriesController extends AppController {

    var $name = 'Catproducts';
    var $components = array(
        'Session', 'Alias', 'Upload'
    );
    var $helpers = array(
        'Editor', 'Published', 'Category'
    );
    function beforeFilter() {
        $this->Catproduct->writeCatProductRootMenu();
        parent::beforeFilter();
    }

    function index() {
        $this->Catproduct->writeCat(0, 0, $html);
        $this->Catproduct->cacheCatList('186', 'catids_thoitrangnu');
        $this->Catproduct->cacheCatList('193', 'catids_thoitrangnam');
        $this->Catproduct->cacheCatList('198', 'catids_thoitrangtreem');
        $this->Catproduct->writeCatProductRootMenu();
        $this->set('title_for_layout', Configure::read('site_name') . ' - Product category');
        $page = isset($this->params['named']['page']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $parent_id = isset($this->params['named']['parent_id']) ? $this->params['named']['parent_id'] : 0;

        $conditions = array();
        $conditions['AND'] = array(
            'parent_id' => $parent_id
        );
        //search
        if ($search) {
            $conditions['OR'] = array(
                'title LIKE' => "%$search%",
            );
        }

        $this->paginate = array(
            'conditions' => $conditions,
            'fields'    =>  array('Catproduct.id', 'Catproduct.title', 'Catproduct.published', 'Catproduct.ordering', 'UserModify.id', 'UserModify.username'),
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Catproduct.ordering' => 'ASC'
            ),
        );
        $data = $this->paginate('Catproduct');
        $total_record   =   ($this->params['paging']['Catproduct']['count'] > Configure::read('limit_list'))    ?
                            Configure::read('limit_list')   :   $this->params['paging']['Catproduct']['count'];

        $this->set('total', $total_record);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('parent_id', $parent_id);
        $this->set('start_page', $page);
    }

    function remove() {
        $params = array_merge($this->params['form'], $this->params['named']);
        $conditions = array(
            "{$this->Catproduct->alias}.{$this->Catproduct->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );

        if ($this->Catproduct->deleteAll($conditions, true)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d Category", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index', 'parent_id'=>$params['parent_id']));
    }

    function edit() {
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Catproduct->id = $params['cid'][0];
        } else {
            $this->Catproduct->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }
        $disable_array = array();
        $this->Catproduct->getChildAllById($this->Catproduct->id, $disable_array);

        $disable_array[] = $this->Catproduct->id;
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');

        if (empty($this->data)) {
            $fields = array(
                'title', 'published', 'id', 'description', 'parent_id'
            );
            $this->data = $this->Catproduct->read($fields);
            $this->set('description', $this->data['Catproduct']['description']);
            $this->set('disable_array', $disable_array);
            $this->set('parent_id', $this->data['Catproduct']['parent_id']);
        }
    }

    function add() {
        $params = array_merge($this->params['form'], $this->params['named']);
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm Danh mục');
        $this->set('parent_id', $params['parent_id']);
    }

    function save() {
        $data = $this->data['Catproduct'];

        //end upload thumbnail
        $data['alias'] = $this->Alias->alias($data['title']);

        if(isset ($data['id'])){
            $this->Catproduct->set('user_modified', $this->Auth->user('id'));
        }else{
            $this->Catproduct->set('user_created', $this->Auth->user('id'));
        }
        if ($this->Catproduct->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Catproduct->invalidFields(), 'error', array(), 'msg');
        }
        
        $this->redirect(array('controller' => 'product_categories', 'action' => 'index', 'parent_id'=>$data['parent_id']));
    }

    function cancel() {
        $this->redirect(array('controller' => 'product_categories', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->Catproduct->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Catproduct->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'product_categories', 'action' => 'index'));
    }

    function saveorder() {
        $parent_id = $this->params['form']['parent_id'];
        $ordering = $this->params['form']['order'];

        foreach ($ordering AS $id => $order) {
            $this->Catproduct->set('id', $id);
            $this->Catproduct->saveField('ordering', $order);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('controller' => 'product_categories', 'action' => 'index', 'parent_id'=>$parent_id));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->Catproduct->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Catproduct->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'product_categories', 'action' => 'index'));
    }

}

?>
